﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Ideaa.Data.Models;
using Ideaa.Data;
using Ideaa.Web.Models;
using Microsoft.AspNet.Identity;
using Ideaa.Services.Interfaces;
using System.Collections;
using AutoMapper;
using Ideaa.Data.Models.Base;

namespace Ideaa.Web.Controllers
{
    public class IdeaController : Controller
    {
        private readonly IIdeaService ideaService;

        public IdeaController(IIdeaService ideaService)
        {
            this.ideaService = ideaService;
        }

        public ActionResult Index()
        {
            var ideas = this.ideaService.GetAll();

            IList<IdeaModel> models = Mapper.Map<IList<Idea>, IList<IdeaModel>>(ideas);

            return View(models);
        }

        // GET: /Idea/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            Idea idea = ideaService.GetIdeaById(id.Value);

            if (idea == null)
            {
                return HttpNotFound();
            }

            IdeaModel model = new IdeaModel();
            model = Mapper.Map<IdeaModel>(idea);

            return View(model);
        }

        // GET: /Idea/Create
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IdeaModel model)
        {
            if (ModelState.IsValid)
            {
                //Idea idea = Mapper.Map<Idea>(model);
                model.UserId = User.Identity.GetUserId();

                //ideaService.Add(idea);
                int ideaId = ideaService.AddIdea(model.Id, model.UserId, model.Title, model.Content);
                return RedirectToAction("Details", new { id = ideaId });
            }

            return View();
        }

        //// GET: /Idea/Edit/5
        //public ActionResult Edit(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Idea idea = db.Ideas.Find(id);
        //    if (idea == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    ViewBag.UserId = new SelectList(db.IdentityUsers, "Id", "UserName", idea.UserId);
        //    return View(idea);
        //}

        //// POST: /Idea/Edit/5
        //// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        //// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include="Id,Title,Content,DateCreatedOnUtc,UserId")] Idea idea)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(idea).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    ViewBag.UserId = new SelectList(db.IdentityUsers, "Id", "UserName", idea.UserId);
        //    return View(idea);
        //}

        //// GET: /Idea/Delete/5
        //public ActionResult Delete(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Idea idea = db.Ideas.Find(id);
        //    if (idea == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(idea);
        //}

        //// POST: /Idea/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    Idea idea = db.Ideas.Find(id);
        //    db.Ideas.Remove(idea);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}
    }
}
